package com.bulletelc.apollopre.module.main.center.invoice.edit;

import android.util.Log;

import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.InvoiceBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.bulletelc.apollopre.utils.MyUtil;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * author: lhl
 * date  : 2018/01/29
 * desc  : TODO
 */

public class EditInvoicePresenter extends BasePresenter<EditInvoiceContract.View> implements EditInvoiceContract.Presenter {
    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void addInvoice(String json) {
        addInvoiceImp(json);
    }

    @Override
    public void getInvoiceDetail(int invoiceId) {
        getInvoiceDetailImp(invoiceId);
    }
    @Override
    public void editInvoice(int invoiceId, String json) {
        editInvoiceImp(invoiceId, json);
    }

    /**
     * 获取发票详情
     *
     * @param invoiceId
     */
    private void getInvoiceDetailImp(int invoiceId) {
        OkGo.<String>post(NetWorkPath.POST_USER_INVOICE_DETAIL + "?id=" + invoiceId)
                .headers(NetWorkPath.HEADER_KEY_AUTH, MyUtil.getAuthToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        parseInvoiceDetail(response);
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorInvoiceDetail(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 解析发票详情数据
     *
     * @param response
     */
    private void parseInvoiceDetail(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    if (isViewAttached()) {
                        Gson gson = new Gson();
                        InvoiceBean invoiceBean = gson.fromJson(obj.getJSONObject("data").toString(), InvoiceBean.class);
                        getView().onSuccessInvoiceDetail(invoiceBean);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailInvoiceDetail(objMeta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorInvoiceDetail("数据解析有误");
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailInvoiceDetail(response.body().toString());
            }
        }
    }

    /**
     * 添加发票
     *
     * @param json
     */
    private void addInvoiceImp(String json) {
        RequestBody body = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), json);
        OkGo.<String>post(NetWorkPath.POST_USER_INVOICE_CREATE)
                .headers(NetWorkPath.HEADER_KEY_AUTH, MyUtil.getAuthToken())
                .upRequestBody(body)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseAddInvoice(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorAddInvoice(response.getException().toString());
                        }
                    }

                });
    }

    /**
     * 解析添加发票的返回数据
     *
     * @param response
     */
    private void parseAddInvoice(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    if (isViewAttached()) {
                        getView().onSuccessAddInvoice();
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailAddInvoice(objMeta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorAddInvoice(e.getMessage().toString());
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailAddInvoice(response.body().toString());
            }
        }
    }

    /**
     * 编辑发票
     */
    private void editInvoiceImp(int invoiceId, String json) {
        Log.i("editInvoiceImp", json);
        RequestBody body = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), json);
        OkGo.<String>post(NetWorkPath.POST_USER_INVOICE_UPDATE + "?id=" + invoiceId)
                .headers(NetWorkPath.HEADER_KEY_AUTH, MyUtil.getAuthToken())
                .upRequestBody(body)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.i("editInvoiceImpres", response.body().toString());
                        parseEditInvoice(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorEditInvoice(response.getException().toString());
                        }
                    }

                });
    }

    /**
     * 解析编辑发票返回的数据
     *
     * @param response
     */
    private void parseEditInvoice(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    if (isViewAttached()) {
                        getView().onSuccessEditInvoice();
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailEditInvoice(objMeta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorEditInvoice(e.getMessage().toString());
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailEditInvoice(response.body().toString());
            }
        }
    }
}
